﻿using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Drawing;
using VIT.Library.Web;
using VIT.Business;
using VIT.Web.UI;

namespace VIT.Web.UserControl
{
    public partial class Contact : VITModule
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // Create a random code and store it in the Session object.
                this.Session["CaptchaImageText"] = GenerateRandomCode();
            }
            loadCaptChaImage();
            loadLangForControl();
            string a = URLTemplatePath;
        }

        private void loadLangForControl()
        {
            btnDoiMa.Text = (string)GetLocalResourceObject("ChangeCode");
            btnSend.Text = (string)GetLocalResourceObject("Send");
            btnExit.Text = (string)GetLocalResourceObject("ReWrite");
        }

        private void loadCaptChaImage()
        {
            HatchBrush HatchBrushAnh = new HatchBrush(HatchStyle.HorizontalBrick, Color.Gray, Color.Yellow);
            HatchBrush HatchBrusText = new HatchBrush(HatchStyle.ZigZag, Color.Red, Color.Red);

            imgCaptcha.Src = MainCore.captChaImage(Session["CaptchaImageText"].ToString(), HatchBrushAnh, HatchBrusText);
        }

        protected void btnDoiMa_Click(object sender, EventArgs e)
        {
            this.Session["CaptchaImageText"] = GenerateRandomCode();
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            // kiem tra ma xac nhan
            if (txtCode.Text != Session["CaptchaImageText"].ToString()) setTrueVisible(lblMaXacNhan);
            else
            {
                // gui mail
                BLConfig c = new BLConfig(); c.LanguageId = Session["language"].ToString(); c.GetConfig();
                string send = MainCore.sendemail(c.Email, txtEmail.Text, txtSubject.Text, inforcus());
                if (send.Length > 0) setTrueVisible(lblThanhCong);
                else setTrueVisible(lblThatBai);
            }
        }

        protected void btnExit_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtPhone.Text = "";
            txtAddress.Text = "";
            txtContent.Text = "";
            txtSubject.Text = "Góp ý";
        }

        private void setTrueVisible(System.Web.UI.HtmlControls.HtmlGenericControl control)
        {
            lblMaXacNhan.Visible = false;
            lblThanhCong.Visible = false;
            lblThatBai.Visible = false;
            lblThongBao.Visible = false;

            control.Visible = true;
        }

        private string inforcus()
        {
            string chuoi = "<center><table>";
            chuoi += "<tr><td style='width:200px'>Tên:</td><td style='width:500px'>" + txtName.Text + "</td></tr>";
            chuoi += "<tr><td style='width:200px'>Email:</td><td style='width:500px'>" + txtEmail.Text + "</td></tr>";
            chuoi += "<tr><td style='width:200px'>Địa chỉ:</td><td style='width:500px'>" + txtAddress.Text + "</td></tr>";
            chuoi += "<tr><td style='width:200px'>Điện thoại:</td><td style='width:500px'>" + txtPhone.Text + "</td></tr>";
            chuoi += "<tr><td style='width:200px'>Nội dung:</td><td style='width:500px'>" + txtContent.Text + "</td></tr>";
            chuoi += "</table><p /><hr /><a href='http://info@dongacs.com'>http://info@dongacs.com</a></center>";
            return chuoi;
        }

        private string GenerateRandomCode()
        {
            Random random = new Random();
            string s = "";
            for (int i = 0; i < 6; i++)
                s = String.Concat(s, random.Next(10).ToString());
            return s;
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
    }
}